/*
 * This file is part of the Refran Library
 * Copyright (C) 2009 César Enrique García
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/** @file
 * $Author: cquike $
 * $Date: 2010-09-29 10:19:45 +0000 (Wed, 29 Sep 2010) $
 * $Revision: 212 $
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <algorithm>
#include "Error.h"
#include "RecipeFactory.h"

Refran::RecipeFactory::RecipeFactory() throw()
{
}

Refran::RecipeFactory::~RecipeFactory()
{
}

void Refran::RecipeFactory::shutdown()
{
    std::lock_guard<std::mutex> guard(m_mutex);
    if(m_instance.get() != 0)
        m_instance.reset();
}

Refran::RecipeFactory& Refran::RecipeFactory::instance()
{
    //This is expensive (http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf#search=%22meyers%20double%20checked%20locking%22)
    //But it is simple.
    std::lock_guard<std::mutex> guard(m_mutex);
    if(m_instance.get() == 0)
        m_instance.reset ( new RecipeFactory);

    return *m_instance;
}

std::vector<std::string>
Refran::RecipeFactory::getRecipeNameList() const
{
    std::lock_guard<std::mutex> guard(m_mutex);
    std::vector<std::string> recipeList;

    auto iter = m_recipeNameVersionCreators.begin();
    for( ; iter!=m_recipeNameVersionCreators.end(); ++iter)
        recipeList.push_back(iter->first);

    return recipeList;
}

std::vector<unsigned int>
Refran::RecipeFactory::getRecipeVersions(const std::string& recipeName) const
{
    //No need to lock, getVersionsCreator_ does it
    auto versions = getVersionsCreator_(recipeName);

    std::vector<unsigned int> versionsList;
    auto iterVersions = versions.begin();
    for( ; iterVersions!=versions.end(); ++iterVersions)
        versionsList.push_back(iterVersions->first);

    return versionsList;
}

void Refran::RecipeFactory::clear() throw ()
{
    std::lock_guard<std::mutex> guard(m_mutex);
    m_recipeNameVersionCreators.clear();
}

void Refran::RecipeFactory::removeCreator(const std::string& recipeName)
{
    std::lock_guard<std::mutex> guard(m_mutex);
    if(m_recipeNameVersionCreators.erase(recipeName) != 1)
      throw Error(__func__,"The Recipe name was not registered");
}

void Refran::RecipeFactory::removeCreator
(const std::string& recipeName, unsigned int version)
{
    //No need to lock, getVersionsCreator_ does it
    auto versions = getVersionsCreator_(recipeName);

    if(versions.erase(version) != 1)
      throw Error(__func__,"The Recipe version was not registered");
}

std::shared_ptr<Refran::Recipe>
Refran::RecipeFactory::createRecipe(const std::string& recipeName) const
{
    //Get all the version for this recipe
    //No need to lock, getVersionsCreator_ does it
    auto versions = getVersionsCreator_(recipeName);

    //Get the version with higest version
    auto targetVersion = std::max_element(versions.begin(), versions.end());

    //Create the recipe
    std::shared_ptr<Refran::Recipe> recipe(targetVersion->second());
    return recipe;
}

std::shared_ptr<Refran::Recipe>
Refran::RecipeFactory::createRecipe
(const std::string& recipeName, unsigned int version) const
{
    //No need to lock, getVersionsCreator_ does it
    auto versions = getVersionsCreator_(recipeName);

    auto targetVersion = versions.find(version);
    if(targetVersion == versions.end())
        throw Error(__func__,"The Recipe version was not registered");

    std::shared_ptr<Refran::Recipe> recipe(targetVersion->second());
    return recipe;
}

std::map<unsigned int, Refran::RecipeFactory::RecipeCreator>
Refran::RecipeFactory::getVersionsCreator_(const std::string& recipeName) const
{
    std::lock_guard<std::mutex> guard(m_mutex);
    auto targetRecipe = m_recipeNameVersionCreators.find(recipeName);
    if(targetRecipe == m_recipeNameVersionCreators.end())
        throw Error(__func__,"The Recipe name was not registered");

    auto versionsCreator = targetRecipe->second;
    return versionsCreator;
}

std::unique_ptr<Refran::RecipeFactory> Refran::RecipeFactory::m_instance(0);
std::mutex Refran::RecipeFactory::m_mutex;
